from math import trunc, log
import sys
import os
from os import system
from binhex import hexbin
def print_blif(func, out_file):
    used = set()
    n = trunc(log(len(func),2))
    out_file.write('.model a'+func+'\n')
    out_file.write('.inputs ')
    for i in xrange(n):
        out_file.write('x'+str(i)+' ')
    out_file.write('\n.outputs y\n')
    
    for i in xrange(len(func)/2):
        t = func[i*2:i*2+2]
        if not 'f'+t in used:
            used.add('f'+t)
            out_file.write(".names x"+str(n-1)+" f"+t+"\n")
            if t.find('1') == -1:
                out_file.write("- 0\n")
            else:
                if t[0]=='1':
                    out_file.write("0 1\n")
                if t[1]=='1':
                    out_file.write("1 1\n")
    
    for i in xrange(n-1):
        out_file.write(".names x" +str(i)+" notx"+str(i)+"\n")
        out_file.write("0 1\n")
    
    for osn in xrange(2,n+1): 
        block = 2**osn
        for i in xrange(len(func)/block):
            t = func[i*block:i*block+block]
            if not "f"+t in used:
                used.add("f"+t)
                g1 = t[0:len(t)/2]
                g2 = t[len(t)/2:]
                if not "uf"+g1 in used:
                    used.add("uf"+g1)
                    out_file.write(".names notx"+str(n-osn)+" f"+g1+" uf"+g1 +"\n")
                    out_file.write("11 1\n")
                if not "vf"+g2 in used:
                    used.add("vf"+g2)
                    out_file.write(".names x"+str(n-osn)+" f"+g2+" vf"+g2 +"\n")
                    out_file.write("11 1\n")
                out_file.write(".names uf"+g1+" vf"+g2+" f"+t +"\n")
                out_file.write("-1 1\n")
                out_file.write("1- 1\n")
    
    out_file.write(".names f" +func+" y\n")
    out_file.write("1 1\n")
mincodes = open("mincodes5.txt","r")

ii = 0 
for j in xrange(12):
    supscript = open('megascript.for'+str(j),'w')
    for i in xrange(100):
        supscript.write("source my/script.for"+str(j*100+i)+'\n' )
supscript = open('megascript.for12','w')
for i in xrange(1200,1229):
    supscript.write("source my/script.for"+str(i)+'\n' )
    

for f in mincodes:        
    if ii % 1000 == 0:
        if not os.path.exists('my/'+str(ii/1000)): 
            os.makedirs('my/'+str(ii/1000))
        if not os.path.exists('my/'+str(ii/1000) + '/blifs'):
            os.makedirs('my/'+str(ii/1000) + '/blifs')
        supscript.close()
        supscript = open('my/script.for'+str(ii/1000),'w') 
    func = bin(int(f,16))[2:]
    func = "0"*(32-len(func))+func
    out_blif = open('my/'+str(ii/1000)+'/blifs/a'+func+'.blif','w')
    print_blif(func, out_blif)
    out_blif.close()
    scripts = ["algebraic","boolean","rugged"]
    #opt_script = open('my/'+str(ii/1000)+'/script.for'+func,'w')
    supscript.write("read_blif my/"+str(ii/1000)+"/blifs/a"+func+".blif\n")
    supscript.write("print_stats\n")
    for algo in scripts:
        #opt_script.write("read_library libs/basis.genlib\n")
        supscript.write("source scripts/script."+algo+'\n')
        supscript.write("map -W\n")
        #opt_script.write("write_blif my/"+str(ii/1000)+"/blifs/opt_"+algo+func+".blif\n")
        supscript.write("print_stats\n")
    #supscript.write('source my/'+str(ii/1000)+'/script.for'+func+'\n')
    ii+=1
supscript = open('megascript.for','w')
for i in xrange(ii/1000+1):
    supscript.write("source my/script.for"+str(i)+'\n' )    
supscript.close()